<?php

namespace App\Http\Controllers\v1;

use App\Models\News;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NewsController extends Controller
{
    public function index(Request $request)
    {
        $pageNum = request()->input('current_page', 1);
        $pageSize = request()->input('page_size', 10);
        $from = ($pageNum - 1)*$pageSize;

        $nodes = News::where('status', 1)->where('org_id', $this->getOrdId());
        $count = $nodes->count();
        $nodes = $nodes->orderby('is_recommend', 'desc')->orderBy('publish_time', 'desc');
        $nodes = $nodes->take($pageSize)->skip($from)->get();
        $list = [];
        foreach($nodes as $value)
        {
            $list[] = News::format($value);
        }

        $pageInfo = [
            "current_page" => intval($pageNum),
            "total_page" => ceil($count / $pageSize),
            "page_size" => intval($pageSize),
            "count" => $count,
        ];

        $data['list'] = $list;
        $data['page'] = $pageInfo;
        $this->setData($data);

        return $this->response();
    }

    public function info(Request $request, $id)
    {
        $news = News::find($id);

        $data = News::format($news);
        $this->setData($data);

        return $this->response();
    }


}
